package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.ITaskListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/10/29.
 * 精选项目分类 控制层
 */
@Controller
@RequestMapping("/taskList")
public class TaskListController {

    @Autowired
    private ITaskListService taskListService;

    /**
     * 跳转到外包项目首页
     * @param request
     * @param keyword 关键字
     * @param firstClass 一级分类
     * @param secondClass 二级分类
     * @return
     */
    @RequestMapping(value = "/taskList", method = RequestMethod.GET)
    public String taskList(HttpServletRequest request,String keyword,String firstClass,String secondClass){


        try {
            //初始化数据(客户信用等级)
            request.setAttribute("vipLevelList",taskListService.findVipLevelset());
            request.setAttribute("keyword",keyword);
            request.setAttribute("firstClass",firstClass);
            request.setAttribute("secondClass",secondClass);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "/taskList/taskList";
    }

    /**
     * 查询项目分类
     *
     * @param parent 任务类型父节点
     * @return
     */
    @RequestMapping(value = "/findTaskTypeByParent", method = RequestMethod.POST)
    public ResponseEntity<?> findTaskTypeByParent(int parent) {



        try {
            List<Map<String,Object>> list =  taskListService.findTaskTypeByParent(parent);

            ResponseEntity<List<Map<String,Object>>> responseEntity = new ResponseEntity<List<Map<String,Object>>>(list, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }


    /**
     * 查询任务信息
     * @param first_class 一级分类
     * @param second_class 二级分类
     * @param budgetRegion 佣金
     * @param durplusDay 截止日期
     * @param item_status 当前状态
     * @return
     */
    @RequestMapping(value = "/findTaskInfo", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findTaskInfo(int rows,int page,String first_class,String second_class,String budgetRegion,
                                          String durplusDay,String item_status,String title,String orderBy,String clientType) {


        PageInfo pageInfo = null;
        try {

            PageHelper.startPage(page, rows);

            Map<String,String> param = new HashMap<String, String>();

            param.put("first_class",first_class);
            param.put("second_class",second_class);
            param.put("budgetRegion",budgetRegion);
            param.put("durplusDay",durplusDay);
            param.put("item_status",item_status);
            param.put("title",title);
            param.put("orderBy",orderBy);
            param.put("clientType",clientType);
            param.put("property",106102 + "");//项目性质，暂时写死


            List<Map<String,Object>> list =  taskListService.selectAllTaskInfo(param);

            pageInfo =  new PageInfo(list);
//            ResponseEntity<List<Map<String,Object>>> responseEntity = new ResponseEntity<List<Map<String,Object>>>(list, HttpStatus.OK);
//            return responseEntity;


        } catch (Exception e) {

            e.printStackTrace();


//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }


        return pageInfo;

    }


    /**
     * 根据输入的一部分名字，模糊匹配并完成联想搜索功能
     * @param name
     * @return
     */
    @RequestMapping(value = "/associativeSearch", method = RequestMethod.POST)
    public ResponseEntity<?> findTaskInfoNameByName(String name) {

        try {
            List<Map<String,Object>> list =  taskListService.findTaskInfoNameByName(name);

            ResponseEntity<List<Map<String,Object>>> responseEntity = new ResponseEntity<List<Map<String,Object>>>(list, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }



}
